import { Component, Injector, ViewEncapsulation } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
  PipelineListDto,
  ComboboxItemDto,
  RawMaterialDto,
  EquipmentInfoDto,
  TechDocInput,
  ProcInfoDto,
} from '@shared/service-proxies/service-proxies';
import {
  PipelineNodeServiceProxy,
  SimpleTechDocListDto,
  TechDocDto
} from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { appModuleAnimation } from '@shared/animations/routerTransition';

@Component({
  selector: 'gy1',
  templateUrl: './gy1.component.html',
  styleUrls: ['./gy1.component.less'],
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()]
})
export class GY1ModalComponent extends AppComponentBase {
  isLoading = false;
  saving = false;
  filterText = '';
  pipe: PipelineListDto = new PipelineListDto();

  techDocs = new Array<SimpleTechDocListDto>();
  selectedTechDoc = new SimpleTechDocListDto();
  boundedTechDoc = new SimpleTechDocListDto();

  equipments = new Array<ComboboxItemDto>();

  material = new Array<RawMaterialDto>();
  equips = new Array<EquipmentInfoDto>();
  procs = new Array<ProcInfoDto>();

  techDocView = new TechDocDto();

  //actBase: ActBaseModalComponent;

  constructor(
    injector: Injector,
    private _taskServiceProxy: PipelineNodeServiceProxy
  ) {
    super(injector);
    //this.actBase = actBase;
  }

  load(pipe: PipelineListDto): void {
    this.pipe = pipe;
    this.isLoading = true;
    this.getTechDoc();
  }

  getTechDoc() {
    //this._taskServiceProxy.getTechDocList(pipe.id).subscribe(result => {
    this._taskServiceProxy
      .getTechDocListForGY(this.pipe.id, this.filterText)
      .subscribe(result => {
        this.techDocs = result.techDocs;
        this.equipments = result.equipments;
        this.selectedTechDoc = result.techDocs.filter(
          el => el.codeId === result.selectedCodeId
        )[0];
        this.boundedTechDoc = result.techDocs.filter(
          el => el.codeId === result.boundedCodeId
        )[0];

        this.getTechDocInfo(result.selectedCodeId);
      });
  }

  rowSelect(event: any) {
    let rowData = event.data;
    this.getTechDocInfo(rowData.codeId);
  }

  save(): void {
    this.saving = true;

    let input = new TechDocInput();
    // input.paiHao = this.material.newPaiHao;
    // input.materialStatus = this.material.newMaterialStatus;
    // input.paiHaoStandard = this.material.newPaiHaoStandard;
    input.pipeId = this.pipe.id;
    input.techDocCode = this.selectedTechDoc.codeId;
    input.equipmentInfos = this.equips;
    input.procInfos=this.procs;
    input.rawMaterialInfos=this.material;

    this._taskServiceProxy
      .boundTechDoc(input)
      .pipe(
        finalize(() => {
          this.saving = false;
        })
      )
      .subscribe(result => {
        this.notify.success('绑定成功');
      });
  }

  private getTechDocInfo(codeId: string) {
    this.isLoading = true;
    this._taskServiceProxy
      .getTechDocInfo(this.pipe.id, codeId)
      .pipe(
        finalize(() => {
          this.isLoading = false;
        })
      )
      .subscribe(result => {
        this.material =result.rawMaterialInfos;
          // result.rawMaterialInfos !== null &&
          // result.rawMaterialInfos.length !== 0
          //   ? result.rawMaterialInfos[0]
          //   : new RawMaterialDto();
        this.equips = result.equipmentInfos;
        this.procs = result.procInfos;
        //console.log(result);
      });
  }
}
